/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package com.civet.myadmin.modules.order.web;

import com.civet.myadmin.common.config.Global;
import com.civet.myadmin.common.persistence.Page;
import com.civet.myadmin.common.web.BaseController;
import com.civet.myadmin.modules.order.entity.GoodsOrder;
import com.civet.myadmin.modules.order.service.GoodsOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商品订单Controller
 * @author Jockey
 * @version 2016-11-20
 */
@Controller
@RequestMapping(value = "${adminPath}/order/customerOrder")
public class CustomerGoodsOrderController extends BaseController {

	@Autowired
	private GoodsOrderService goodsOrderService;
	
	@RequiresPermissions("order:customerOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(GoodsOrder goodsOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GoodsOrder> page = goodsOrderService.findPage(new Page<GoodsOrder>(request, response), goodsOrder); 
		model.addAttribute("page", page);
		return "modules/order/customerOrderList";
	}

	@RequiresPermissions("order:customerOrder:view")
	@RequestMapping(value = "form")
	public String form(GoodsOrder goodsOrder, Model model) {
	    if (goodsOrder.getId() != null){
            goodsOrder = goodsOrderService.get(goodsOrder.getId());
        }
		model.addAttribute("goodsOrder", goodsOrder);
		return "modules/order/customerOrderForm";
	}

	@RequiresPermissions("order:customerOrder:edit")
	@RequestMapping(value = "save")
	public String save(GoodsOrder goodsOrder, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, goodsOrder)){
			return form(goodsOrder, model);
		}
		goodsOrderService.save(goodsOrder);
		addMessage(redirectAttributes, "保存商品订单成功");
		return "redirect:"+Global.getAdminPath()+"/order/customerOrder/?repage";
	}
	
	@RequiresPermissions("order:customerOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(GoodsOrder goodsOrder, RedirectAttributes redirectAttributes) {
		goodsOrderService.delete(goodsOrder);
		addMessage(redirectAttributes, "删除商品订单成功");
		return "redirect:"+Global.getAdminPath()+"/order/customerOrder/?repage";
	}

}